/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;
import Model.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author Pisarchik_S
 */
public class EditCategory implements CommandListener , ItemCommandListener {
    Dispatcher m_Dispatcher;
    CModel m_Model;

    public EditCategory(Dispatcher dispatcher, CModel model)
    {
        m_Dispatcher = dispatcher;
        m_Model = model;
    }

    Command m_CancelCommand = new Command("Скасаваць", Command.CANCEL, 0);
    Command m_OkCommand = new Command("Ужыць", Command.OK, 0);

    CCategory m_EditedCategory;

    TextField m_CategoryItem;
    StringItem m_DelButton;
    Command m_DelCommand = new Command("Выдаліць", Command.ITEM, 0);

    public void Init(CCategory category)
    {
        m_EditedCategory = category;

        Form form = new Form("Рэдагаваць катэгорыю");
        m_CategoryItem = new TextField("Назва ", m_EditedCategory.GetName(), 12, TextField.PLAIN);
        m_CategoryItem.setDefaultCommand(m_OkCommand);
        m_CategoryItem.setItemCommandListener(this);
        form.append(m_CategoryItem);


        m_DelButton = new StringItem("", "Выдаліць катэгорыю", StringItem.BUTTON);
        m_DelButton.setDefaultCommand(m_DelCommand);
        m_DelButton.setItemCommandListener(this);
        form.append(m_DelButton);


        form.addCommand(m_CancelCommand);
        form.setCommandListener(this);

        m_Dispatcher.GetDisplay().setCurrent(form);
    }

    public void commandAction(Command c, Displayable s)
    {
        if (c == m_CancelCommand)
        {
            m_Dispatcher.GetShowCategories().Init();
        }
    }

    public void commandAction(Command c, Item item)
    {
        if (c == m_DelCommand)
        {
            m_Model.RemoveCategory(m_EditedCategory);
            m_Dispatcher.GetShowCategories().Init();
        }
        if (c == m_OkCommand)
        {
            //CCurrency cur = m_model.m_Curencies[m_Currency.getSelectedIndex()];
            m_EditedCategory.SetName(m_CategoryItem.getString());
            m_Dispatcher.GetShowCategories().Init();
        }
    }
}
